﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Security;
using System.Text;
using System.Threading;

namespace EBA.Framework
{
    /// <summary>
    /// 进程插件启动器。在独立进程中启动插件
    /// </summary>
    public class RemotingProcessBundleLauncher<TBundle> : IBundleLauncher where TBundle : IBundle
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public RemotingProcessBundleLauncher()
        {
            var configFile = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            this.ConfigurationFileName = configFile.Name;
        }


        public string ConfigurationFileName
        {
            get;
            set;
        }

        /// <summary>
        /// 远程插件所在进程
        /// </summary>
        Process BundleProcess { get; set; }

        public void Run(string runtime, string startupPath, string bundleId)
        {
            var launcher = Path.Combine(startupPath, "BundleLauncher.exe");

            this.BundleProcess = new Process();
            this.BundleProcess.StartInfo.UseShellExecute = false;//不使用系统外壳程序启动
            this.BundleProcess.StartInfo.CreateNoWindow = true;//不创建窗口
            this.BundleProcess.StartInfo.FileName = launcher;
            //this.BundleProcess.StartInfo.UserName = "Test";
            //string strPWD = "870415yw"; 
            //SecureString password = new SecureString(); 
            //foreach (char c in strPWD.ToCharArray()) 
            //{ password.AppendChar(c); }
            //this.BundleProcess.StartInfo.Password = password;
            this.BundleProcess.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            this.BundleProcess.StartInfo.LoadUserProfile = true;
            this.BundleProcess.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", runtime, bundleId, typeof(TBundle).AssemblyQualifiedName, typeof(TBundle).Assembly.FullName);
            this.BundleProcess.Start();
        }

        #region Dispose
        private bool _Disposed = false;
        /// <summary>
        /// 析构函数，释放资源。
        /// </summary>
        ~RemotingProcessBundleLauncher()
        {
            Dispose(false);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }


        /// <summary>
        /// 手动释放资源
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_Disposed)
            {
                if (disposing)
                {
                    if (this.BundleProcess != null)
                    {
                        this.BundleProcess.Kill();
                        this.BundleProcess.Dispose();
                        this.BundleProcess = null;
                    }

                    GC.SuppressFinalize(this);
                }
                _Disposed = true;
            }
        }
        #endregion





    }
}
